using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Granite.Data;

namespace LeadTracker.Models
{
	/// <summary>
	/// Data repository for LeadComment
	/// </summary>
	public class LeadCommentRepository
	{
		private readonly Connection m_Connection;

		/// <summary>
		/// Create a new data repository for LeadComment
		/// </summary>
		/// <param name="connection">Connection for making database/service calls</param>
		public LeadCommentRepository(Connection connection)
		{
			m_Connection = connection;
		}

		/// <summary>
		/// Saves a new LeadComment. If the storage logic modifies values, the leadComment will be updated.
		/// </summary>
		/// <param name="leadComment">This is a leadComment containing the new values</param>
		public void Insert(LeadCommentModel leadComment, int auditUserKey, Connection connection = null)
		{

			if (leadComment.HasErrors)
				throw new ValidationException("LeadComment has validation errors");

			var cmd = new ProcedureCall("dbo.LeadCommentInsertModel");
			cmd.Parameters.AutoPopulate("Insert", leadComment);
			cmd.Add("Audit_UserKey", auditUserKey);
			leadComment.Update((connection ?? m_Connection).ExecuteRow(cmd));
		}

		/// <summary>
		/// Updates an existing LeadComment. If the storage logic also modifies values, the leadComment will be updated.
		/// </summary>
		/// <param name="leadComment">This is a leadComment containing the complete set of values</param>
		/// <remarks>Partial updates cannot be performed via this method.</remarks>
		public void Update(LeadCommentModel leadComment, int auditUserKey, Connection connection = null)
		{
			if (leadComment.HasErrors)
				throw new ValidationException("LeadComment has validation errors");

			var cmd = new ProcedureCall("dbo.LeadCommentUpdateModel");
			cmd.Parameters.AutoPopulate("Update", leadComment);
			cmd.Add("Audit_UserKey", auditUserKey);
			leadComment.Update((connection ?? m_Connection).ExecuteRow(cmd));
		}


		/// <summary>
		/// Overwrites any changes in a LeadComment with what is stored in the database. 
		/// </summary>
		/// <param name="leadComment">This is a leadComment to be filled.</param>
		/// <remarks>Only the fields labled with Action("Select") are needed.</remarks>
		public void Reload(LeadCommentModel leadComment, int auditUserKey, Connection connection = null)
		{
			var cmd = new ProcedureCall("dbo.LeadCommentSelecteModel");
			cmd.Parameters.AutoPopulate("Select", leadComment);
			cmd.Add("Audit_UserKey", auditUserKey);
			leadComment.Update((connection ?? m_Connection).ExecuteRow(cmd));
		}

		/// <summary>
		/// Deleted the indicated LeadComment.
		/// </summary>
		/// <param name="leadComment">Primary key of a LeadComment to be deleted.</param>
		public void DeleteLeadComment(int? leadCommentKey, int auditUserKey, Connection connection = null)
		{
			var cmd = new ProcedureCall("dbo.LeadCommentDelete");
			cmd.Add("LeadCommentKey", leadCommentKey);
			cmd.Add("Audit_UserKey", auditUserKey);
			(connection ?? m_Connection).ExecuteNonQuery(cmd);
		}

		/// <summary>
		/// Loads the indicated LeadComment.
		/// </summary>
		/// <param name="leadComment">Primary key of a LeadComment to be loaded.</param>
		public LeadCommentModel SelectLeadComment(int? leadCommentKey, int auditUserKey, Connection connection = null)
		{
			var cmd = new ProcedureCall("dbo.LeadCommentSelectModel");
			cmd.Add("LeadCommentKey", leadCommentKey);
			cmd.Add("Audit_UserKey", auditUserKey);
			return new LeadCommentModel((connection ?? m_Connection).ExecuteRow(cmd));
		}

		///// <summary>
		///// Selects all of the rows for use in a drop-down list
		///// </summary>
		///// <returns></returns>
		//public List<LeadCommentToken> SelectTokens(int auditUserKey, Connection connection = null)
		//{
		//	var cmd = new ProcedureCall("dbo.LeadCommentSelectAllTokens");
		//	cmd.Add("Audit_UserKey", auditUserKey);
		//	var table = (connection ?? m_Connection).ExecuteTable(cmd);
		//	var result = new List<LeadCommentToken>();
		//	foreach(var row in table)
		//		result.Add(new LeadCommentToken(row));
		//	return result;
		//}
	}
}

